//
//  ViewController.m
//  GuideRoute
//
//  Created by dllo on 15/9/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import <AMapNaviKit/MAMapKit.h>
#import <AMapNaviKit/AMapNaviKit.h>
#import "NaviPointAnnotation.h"
//typedef NS_ENUM(NSInteger, NavigationTypes)
//{
//    NavigationTypeNone = 0,
//    NavigationTypeSimulator, // 模拟导航
//    NavigationTypeGPS,       // 实时导航
//};

//typedef NS_ENUM(NSInteger, TravelTypes)
//{
//    TravelTypeCar = 0,      // 驾车方式
//    TravelTypeWalk,         // 步行方式
//};


@interface ViewController ()<MAMapViewDelegate,AMapNaviManagerDelegate,AMapNaviViewControllerDelegate>
@property (nonatomic, strong) MAMapView *mapView;

@property (nonatomic, strong) AMapNaviManager *naviManager;//导航管理类:AMapNaviManager是导航管理类，提供计算规划路径、开启导航等相关方法


@property (nonatomic, strong) AMapNaviPoint* startPoint;//起点
@property (nonatomic, strong) AMapNaviPoint* endPoint;//终点

//此类用于定义一个由多个点相连的多段线
@property (nonatomic, strong)MAPolyline *polyline;

@property (nonatomic) BOOL calRouteSuccess; // 指示是否算路成功

//@property (nonatomic) NavigationTypes naviType;//导航模式:分为实时和模拟
//@property (nonatomic) TravelTypes travelType;//出行方式:分为驾车和步行

@property (nonatomic, strong) NSArray *annotations;


@end

@implementation ViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _startPoint = [AMapNaviPoint locationWithLatitude:39.989614 longitude:116.481763];
        _endPoint   = [AMapNaviPoint locationWithLatitude:39.983456 longitude:116.315495];
        
        
        //起点的标注
        NaviPointAnnotation
        *beginAnnotation = [[NaviPointAnnotation alloc] init];
        
        [beginAnnotation setCoordinate:CLLocationCoordinate2DMake(_startPoint.latitude, _startPoint.longitude)];
        beginAnnotation.title        = @"起始点";
        beginAnnotation.navPointType = NavPointAnnotationStart;
        
        
        //终点的标注
        NaviPointAnnotation *endAnnotation = [[NaviPointAnnotation alloc] init];
        
        [endAnnotation setCoordinate:CLLocationCoordinate2DMake(_endPoint.latitude, _endPoint.longitude)];
        
        endAnnotation.title        = @"终点";
        endAnnotation.navPointType = NavPointAnnotationEnd;
        
        self.annotations = @[beginAnnotation, endAnnotation];
        
        
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    
     //.多个controller共用一个mapview的时候，mapview在其他controller中添加后会从本controller中删除，需重新添加
    
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    [_mapView setDelegate:self];
    
    
    //把存在属性数组的里的大头针加到地图上
    [self.mapView addAnnotations:self.annotations];
    [self.view addSubview:_mapView];
    
#pragma mark --
//    UIButton *simuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    simuBtn.frame = CGRectMake(0, 40, 100, 40);
//    simuBtn.backgroundColor = [UIColor blackColor];
//    simuBtn.alpha = 0.7;
//    [simuBtn setTitle:@"模拟导航" forState:UIControlStateNormal];
//    [simuBtn addTarget:self action:@selector(simulatorNavi:) forControlEvents:UIControlEventTouchUpInside];
//    
//    [self.view addSubview:simuBtn];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //初始化导航管理
    self.naviManager = [[AMapNaviManager alloc] init];
    self.naviManager.delegate = self;
    
    NSArray *startPoints = @[_startPoint];
    NSArray *endPoints   = @[_endPoint];
    
    
    [self.naviManager calculateDriveRouteWithStartPoints:startPoints endPoints:endPoints wayPoints:nil drivingStrategy:0];
    
    
    

    
    
    
}



//导航控制器类AMapNaviManager的delegate方法
- (void)naviManagerOnCalculateRouteSuccess:(AMapNaviManager *)naviManager
{
    //当前导航路径的信息
    [self showRouteWithNaviRoute:[naviManager naviRoute]];
    _calRouteSuccess = YES;
}


//自定义系统大头针的类,插针
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[NaviPointAnnotation class]])
    {
        static NSString *annotationIdentifier = @"annotationIdentifier";
        
        MAPinAnnotationView *pointAnnotationView = (MAPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
        if (pointAnnotationView == nil)
        {
            pointAnnotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation
                                                                  reuseIdentifier:annotationIdentifier];
        }
        //动画效果
        pointAnnotationView.animatesDrop   = YES;
        
        pointAnnotationView.canShowCallout = YES;
        

        
        NaviPointAnnotation *navAnnotation = (NaviPointAnnotation *)annotation;
        
        if (navAnnotation.navPointType == NavPointAnnotationStart)
        {
            [pointAnnotationView setPinColor:MAPinAnnotationColorGreen];
        }
        else if (navAnnotation.navPointType == NavPointAnnotationEnd)
        {
            [pointAnnotationView setPinColor:MAPinAnnotationColorRed];
        }
        return pointAnnotationView;
    }
    
    return nil;
}


- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id<MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MAPolyline class]])
    {//该类是地图覆盖物View的基类, 提供绘制overlay的接口但并无实际的实现
        //创建polylineView给属性赋值
        MAPolylineView *polylineView = [[MAPolylineView alloc] initWithPolyline:overlay];
        polylineView.lineWidth   = 5.0f;
        polylineView.strokeColor = [UIColor redColor];
        return polylineView;
    }
    return nil;
}




- (void)showRouteWithNaviRoute:(AMapNaviRoute *)naviRoute
{

    NSUInteger coordianteCount = [naviRoute.routeCoordinates count];
    
    
    CLLocationCoordinate2D coordinates[coordianteCount];
    for (int i = 0; i < coordianteCount; i++)
    {//AMapNaviPoint数组，导航路线的所有形状点
        AMapNaviPoint *aCoordinate = [naviRoute.routeCoordinates objectAtIndex:i];
        coordinates[i] = CLLocationCoordinate2DMake(aCoordinate.latitude, aCoordinate.longitude);
    }
    //根据数组里的坐标画出polyline,并加在视图类上
    _polyline = [MAPolyline polylineWithCoordinates:coordinates count:coordianteCount];
    [self.mapView addOverlay:_polyline];
}




#pragma mark ---

//- (void)simulatorNavi:(id)sender
//{
//    if (_calRouteSuccess)
//    {
//        AMapNaviViewController *naviViewController = [[AMapNaviViewController alloc]
//                                                      initWithMapView:self.mapView delegate:self];
//        [self.naviManager presentNaviViewController:naviViewController animated:YES];
//    }
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
